<?php
/**
 * ProjectName: Grapes.
 * Created by LemonZ.
 * FileName: Advertisement.php
 * Date: 2019/2/14
 * Time: 21:35
 * Description:
 */

namespace app\admin\controller;


use app\common\controller\Admin;
use app\common\model\AdvertPosition as AdvertPositionModel;
use app\common\model\Advertisement as AdvertisementModel;
use app\common\model\Article;
use app\common\model\ArticleType;
use app\common\model\Goods;
use think\facade\Request;

class Advertisement extends Admin
{
    /**
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        if(Request::isAjax())
        {
            $advertisement = new AdvertisementModel();
            return  $advertisement->tableData(input('param.'));
        }
        return $this->fetch();
    }

    /**
     * 广告添加信息
     * @return array|mixed
     */
    public function add()
    {
        $advertisement = new AdvertisementModel();
        if(Request::isPost())
        {
            return $advertisement->addData(input('param.'));
        }
        $positionModel = new AdvertPositionModel();
        $positionList = $positionModel->select();
        return $this->fetch('add',[
            'list' => $positionList,
            'type'=>config('params.advertType')['type']
        ]);
    }

    /**
     * 广告信息编辑
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $advertisement = new AdvertisementModel();
        if(Request::isPost())
        {
            return $advertisement->saveData(input('param.'));
        }
        $advertisementModel = new AdvertisementModel();
        $info = $advertisementModel->where('id',input('param.id/d'))->find();
        if (!$info)
        {
            return error_code(10002);
        }
        $positionModel = new AdvertPositionModel();
        $position = $positionModel->where('id',$info['position_id'])->find();
        return $this->fetch('edit',[
            'position'=>$position,
            'info'=>$info,
            'type'=>config('params.advertType')['type']
        ]);
    }

    /**
     * 获取对应商户的广告位列表
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function positionList()
    {
        $positionModel = new AdvertisementModel();
        return $positionModel->field('id,code,name')->select();
    }

    /**
     * 广告信息删除
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        $advertisement = new AdvertisementModel();
        $result = ['status'=>false,'msg'=>'删除失败','data'=>''];
        if ($advertisement->where('id',input('param.id/d'))->delete())
        {
            $result['status'] = true;
            $result['msg'] = '删除成功';
        }
        return $result;
    }

    /**
     * 加载商品列表模板
     * @return mixed
     */
    public function getGoods()
    {
        $this->view->engine->layout(false);
        return $this->fetch('getGoods');
    }

    /**
     * 加载文章列表模板
     * @return mixed
     */
    public function getArticle()
    {
        $this->view->engine->layout(false);
        return $this->fetch('getArticle');
    }

    /**
     * @return mixed
     */
    public function getArticleType()
    {
        $this->view->engine->layout(false);
        return $this->fetch('getArticleType');
    }

    /**
     * @return array|null|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function articleInfo()
    {
        $articleModel = new Article();
        return $articleModel->field('id,title')->where('id',input('param.id'))->find();
    }

    /**
     * @return array|null|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function goodsInfo()
    {
        $goodsModel = new Goods();
        return $goodsModel->field('id,name')->where('id',input('param.id'))->find();
    }

    /**
     * @return array|null|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function articleTypeInfo()
    {
        $articleType = new ArticleType();
        return $articleType->field('id,type_name')->where('id',input('param.id'))->find();
    }
}